#include "databaseinfodialog.h"
#include "databasespecificmanager.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QTabWidget>
#include <QTreeWidget>
#include <QTextEdit>
#include <QDialogButtonBox>
#include <QHeaderView>
#include <QSqlDatabase>
#include <QPushButton>
#include <QApplication>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>

DatabaseInfoDialog::DatabaseInfoDialog(const QSqlDatabase &db, QWidget *parent)
    : QDialog(parent)
    , m_database(db)
{
    m_dbManager = new DatabaseSpecificManager(this);
    m_dbManager->setDatabase(db);
    
    setupUI();
    loadDatabaseInfo();
}

void DatabaseInfoDialog::refreshInfo()
{
    loadDatabaseInfo();
}

void DatabaseInfoDialog::setupUI()
{
    setWindowTitle(tr("Database Information"));
    setModal(true);
    resize(600, 400);
    
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    
    m_tabWidget = new QTabWidget;
    
    // 函数标签页
    QWidget *functionsWidget = new QWidget;
    QVBoxLayout *functionsLayout = new QVBoxLayout(functionsWidget);
    m_functionsTree = new QTreeWidget;
    m_functionsTree->setHeaderLabels({tr("Function Name")});
    functionsLayout->addWidget(m_functionsTree);
    m_tabWidget->addTab(functionsWidget, tr("Functions"));
    
    // 存储过程标签页
    QWidget *proceduresWidget = new QWidget;
    QVBoxLayout *proceduresLayout = new QVBoxLayout(proceduresWidget);
    m_proceduresTree = new QTreeWidget;
    m_proceduresTree->setHeaderLabels({tr("Procedure Name")});
    proceduresLayout->addWidget(m_proceduresTree);
    m_tabWidget->addTab(proceduresWidget, tr("Procedures"));
    
    // 触发器标签页
    QWidget *triggersWidget = new QWidget;
    QVBoxLayout *triggersLayout = new QVBoxLayout(triggersWidget);
    m_triggersTree = new QTreeWidget;
    m_triggersTree->setHeaderLabels({tr("Trigger Name")});
    triggersLayout->addWidget(m_triggersTree);
    m_tabWidget->addTab(triggersWidget, tr("Triggers"));
    
    // 视图标签页
    QWidget *viewsWidget = new QWidget;
    QVBoxLayout *viewsLayout = new QVBoxLayout(viewsWidget);
    m_viewsTree = new QTreeWidget;
    m_viewsTree->setHeaderLabels({tr("View Name")});
    viewsLayout->addWidget(m_viewsTree);
    m_tabWidget->addTab(viewsWidget, tr("Views"));
    
    // 数据库参数标签页
    QWidget *parametersWidget = new QWidget;
    QVBoxLayout *parametersLayout = new QVBoxLayout(parametersWidget);
    m_parametersText = new QTextEdit;
    m_parametersText->setReadOnly(true);
    parametersLayout->addWidget(m_parametersText);
    m_tabWidget->addTab(parametersWidget, tr("Parameters"));
    
    // 数据库版本标签页
    QWidget *versionWidget = new QWidget;
    QVBoxLayout *versionLayout = new QVBoxLayout(versionWidget);
    m_versionText = new QTextEdit;
    m_versionText->setReadOnly(true);
    versionLayout->addWidget(m_versionText);
    m_tabWidget->addTab(versionWidget, tr("Version"));
    
    mainLayout->addWidget(m_tabWidget);
    
    // 按钮
    QHBoxLayout *buttonLayout = new QHBoxLayout;
    QPushButton *refreshButton = new QPushButton(tr("Refresh"));
    connect(refreshButton, &QPushButton::clicked, this, &DatabaseInfoDialog::refreshInfo);
    buttonLayout->addWidget(refreshButton);
    buttonLayout->addStretch();
    
    m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
    buttonLayout->addWidget(m_buttonBox);
    
    mainLayout->addLayout(buttonLayout);
    
    connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
}

void DatabaseInfoDialog::loadDatabaseInfo()
{
    loadFunctions();
    loadProcedures();
    loadTriggers();
    loadViews();
    loadDatabaseParameters();
    
    // 显示数据库版本
    QString version = m_dbManager->getDatabaseVersion();
    m_versionText->setPlainText(version);
}

void DatabaseInfoDialog::loadFunctions()
{
    m_functionsTree->clear();
    
    QStringList functions = m_dbManager->getFunctions();
    for (const QString &function : functions) {
        QTreeWidgetItem *item = new QTreeWidgetItem(m_functionsTree);
        item->setText(0, function);
    }
    
    m_functionsTree->header()->resizeSections(QHeaderView::ResizeToContents);
}

void DatabaseInfoDialog::loadProcedures()
{
    m_proceduresTree->clear();
    
    QStringList procedures = m_dbManager->getProcedures();
    for (const QString &procedure : procedures) {
        QTreeWidgetItem *item = new QTreeWidgetItem(m_proceduresTree);
        item->setText(0, procedure);
    }
    
    m_proceduresTree->header()->resizeSections(QHeaderView::ResizeToContents);
}

void DatabaseInfoDialog::loadTriggers()
{
    m_triggersTree->clear();
    
    QStringList triggers = m_dbManager->getTriggers();
    for (const QString &trigger : triggers) {
        QTreeWidgetItem *item = new QTreeWidgetItem(m_triggersTree);
        item->setText(0, trigger);
    }
    
    m_triggersTree->header()->resizeSections(QHeaderView::ResizeToContents);
}

void DatabaseInfoDialog::loadViews()
{
    m_viewsTree->clear();
    
    QStringList views = m_dbManager->getViews();
    for (const QString &view : views) {
        QTreeWidgetItem *item = new QTreeWidgetItem(m_viewsTree);
        item->setText(0, view);
    }
    
    m_viewsTree->header()->resizeSections(QHeaderView::ResizeToContents);
}

void DatabaseInfoDialog::loadDatabaseParameters()
{
    m_parametersText->clear();
    
    QVariantMap parameters = m_dbManager->getDatabaseParameters();
    QString text;
    
    if (parameters.contains("data")) {
        QJsonArray dataArray = parameters["data"].toJsonArray();
        for (const QJsonValue &value : dataArray) {
            QJsonObject obj = value.toObject();
            QStringList keys = obj.keys();
            for (const QString &key : keys) {
                text += QString("%1: %2\n").arg(key).arg(obj[key].toString());
            }
            text += "\n";
        }
    }
    
    m_parametersText->setPlainText(text);
}